package com.nmsl.statistic.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nmsl.statistic.constants.Constants;
import com.nmsl.statistic.domain.RedisPage;
import com.nmsl.statistic.domain.RetailAtt;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class RedisUtil {
    public static  <T> RedisPage getMedicinesRanking(RedisTemplate redisTemplate,Long curentPage,Long size,String key,Class<T> type){

        List<T> l = new ArrayList<>();
        Long staert ;
        Long max = redisTemplate.opsForZSet().size(key);
        if (max-(size*curentPage)< 0){
            staert = 0l;
        }else {
            staert = max-(size*curentPage);
        }

        Set set = redisTemplate.opsForZSet().range(key,staert,max-1- (size*(curentPage-1)));

        System.out.println("开始"+(max-(size*curentPage)) );
        System.out.println( "结束" +(max-(size*(curentPage-1))));
        System.out.println(set.size());
        set.forEach((value) -> {
            T t = JSONObject.parseObject(JSON.toJSONString(value), type);
            l.add(t);
        });
       return RedisPage.build(l,max,size,curentPage,key);
    }
    public static  void updataMedicinesRanking(RedisTemplate redisTemplate,String key,Object c,Integer increase){
        Double aDouble = redisTemplate.opsForZSet().incrementScore(key, c.toString(), increase);
    }
}
